//def
function change_page(offset, per_page)
{
    var current_uri = $('input[name=url]').val();

    var page = offset / per_page + 1;
    var url = '';
    if (current_uri.search('@') != -1)
        url = current_uri.replace('@', '/trang-' + page)
    else
        url = current_uri + '/trang-' + page;

    location.href = url;
    return;
}

function set_slide_show()
{
    $('.home-banner-sl').nivoSlider({
        effect: 'fold', // Specify sets like: 'fold,fade,sliceDown'
        animSpeed: 500, // Slide transition speed
        pauseTime: 3000, // How long each slide will show
        startSlide: 0, // Set starting Slide (0 index)
        directionNav: false, // Next & Prev navigation
        controlNav: false, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        pauseOnHover: true, // Stop animation while hovering
        manualAdvance: false, // Force manual transitions
        randomStart: false, // Start on a random slide
    });
}
function run_slide_products_related($ele) {
    $($ele).jcarousel({
        wrap: 'last',
        visible: 4,
        scroll: 1,
        initCallback: function(carousel) {
            carousel.buttonNext.bind('click', function() {
                carousel.startAuto(5);
            });
            carousel.buttonPrev.bind('click', function() {
                carousel.startAuto(5);
            });

            carousel.clip.hover(function() {
                carousel.stopAuto(5);
            }, function() {
                carousel.startAuto(5);
            });
        }
    });
}

function get_district(obj) {
    var parent_id = $('select[name=city]').val();

    $.ajax(
            {
                type: 'post',
                url: '/get_district',
                data: {
                    'is-ajax': 1,
                    'parent_id': parent_id
                },
                success: function(responseText) {
                    if ($('#district'))
                        $('#district').html(responseText);
                }
            });
}

function tabs()
{
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("tab_active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("tab_active"); //Remove any "active" class
        $(this).addClass("tab_active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });
}

function product_contact(uri, title) {
    if (uri != '') {
        window.location.href = '/lien-he' + '?title=' + encodeURIComponent(title) + '&uri=' + encodeURIComponent(uri);
    }
    return false;
}
function show_fancy_box()
{
    $("a[rel=example_group]").fancybox({
        'overlayShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'titlePosition': 'over',
        'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
        }
    });
}

function autoCutStr(prefix) {
    if (!prefix || prefix === undefined) {
        prefix = "autoCutStr_";
    }
    $('[class^="' + prefix + '"]').each(function() {
        if ($(this).length > 0) {
            var str = $(this).html();
            var len = parseInt($(this).attr("class").substr($(this).attr("class").lastIndexOf("_") + 1));
            var length = str.length;
            if (length > len) {
                if (str.charAt(len) == ' ') {
                    str = str.substr(0, len);
                }
                else {
                    str = str.substr(0, len);
                    str = str.substr(0, str.lastIndexOf(" "));
                }
                $(this).html(str + "...");
            }
        }
    });
}
/**
 * Sticky menu left
 */
function sticky_leftmenu() {
    var fixed = false;
    $left_pos = $('.wrap-left').offset();
    $(document).scroll(function() {
        if ($(this).scrollTop() >= 200) {
            if (!fixed) {
                fixed = true;
                $('.left-menu').css({position: 'fixed'}).animate({top: 120}, 500);
            }
        } else {
            if (fixed) {
                fixed = false;
//                $('.left-menu').animate({top: 120}, 500);
                $('.left-menu').css({position: 'static'});
            }
        }
    });
}
$(function() {
//    set_slide_show();
    autoCutStr();
    sticky_leftmenu();
});